package com.hmall.search.web;

import com.hmall.common.feign.ItemClient;
import com.hmall.search.pojo.Order;
import com.hmall.search.pojo.OrderPayload;
import com.hmall.search.service.IOrderService;
import com.hmall.search.service.OrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("{id}")
    public Order queryOrderById(@PathVariable("id") Long orderId) {
        return orderService.getById(orderId);
    }


    @PostMapping
    public String submit(@RequestBody OrderPayload orderPayload) {

        Order order = orderService.submit(orderPayload);
        //创建消息
        Message message = MessageBuilder
                .withBody(order.getId().toString().getBytes(StandardCharsets.UTF_8))
                .setHeader("x-delay", 10000)
                .build();
        //消息Id封装
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("delay.direct","delay",message,correlationData);

        return order.getId() + ":";

    }



}
